
package com.mekegi.whoanthro;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import com.mekegi.whoanthro.Sql.ChildAdapter;
import com.mekegi.whoanthro.Sql.MeasureAdapter;
import java.util.Date;
import java.util.GregorianCalendar;

public class MainActivity extends Activity implements View.OnClickListener, MenuItem.OnMenuItemClickListener, View.OnCreateContextMenuListener {

    protected static final int FOR_CHILD_ACTIVITY_RESULT = 0xDEC0DED;
    protected static final int FOR_MEASURES_ACTIVITY_RESULT = 0xC0DED;
    public static final String KEY_FOR_RESULT_CHILD_ACTIVITY = "key_for_result_child";
    public static final String KEY_FOR_RESULT_MEASURES_ACTIVITY = "key_for_result_measures";
    protected TextView mText;
    protected ChildAdapter childAdapter;
    protected Long mSelectedChildId = 0L;
    /**
     * @since 26.02.13 22:52
     * @author Arsen Abdusalamov
     */
    protected class ChildNamesHelper implements MenuItem.OnMenuItemClickListener {

        protected Activity parentActivity;

        public ChildNamesHelper(Activity parentActivity) {
            this.parentActivity = parentActivity;
        }

        @Override 
        public boolean onMenuItemClick(MenuItem item) {
            ChildAdapter adapter = new ChildAdapter(parentActivity);
            Child child = (Child)adapter.findItemByPK(item.getItemId());
            ((MainActivity)parentActivity).setDefaultChild(child.getId());
            return true;
        }

        /**
         * @since 26.02.13 13:52
         * @param subMenu
         * @param child
         * @return
         */
        public MenuItem addMenuItem(SubMenu subMenu, Child child) {
            MenuItem menuItem = subMenu.add(Menu.NONE, child.getId().intValue(), Menu.NONE, child.getFullName());
            menuItem.setOnMenuItemClickListener(this);
            return menuItem;
        }
    }

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_measure);
        childAdapter = new ChildAdapter(this);
        mText = (TextView)findViewById(R.id.text);
        //((Button)findViewById(R.id.measure_results)).setOnClickListener(this);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        int count = childAdapter.getCount();
        ChildNamesHelper helper = new ChildNamesHelper(this);
        MenuItem childsNamesMenu = menu.findItem(R.id.child_names);
        SubMenu childsNamesSubMenu = menu.findItem(R.id.child_names).getSubMenu();
        for (int i = 0; i < count; i++) {
            Child child = (Child)childAdapter.getItem(i);
            helper.addMenuItem(childsNamesSubMenu, child);
        }
        if (mSelectedChildId > 0) {
            Child child = (Child)childAdapter.findItemByPK(mSelectedChildId);
            childsNamesMenu.setTitle(child.getFullName());
        } else {
            childsNamesMenu.setTitle(R.string.select_child);
        }

        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        return this.onOptionsItemSelected(item);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        return this.onOptionsItemSelected(item);
    }

    /**
     * @author duburlan@gmail.com
     * @since 05.01.13 1:58
     * @param View v
     */
    public void onClick(View v) {
        Integer x = 10;
        x += 10;
        switch (v.getId()) {
            case R.id.new_measure_results:
                resultMeasures();
                break;
            default:
                mText.setText(((Button)v).getText());
                break;
            //todo;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        String result = "";

        // Handle item selection
        switch (item.getItemId()) {
            case R.id.add_child:
                addChildLayout();
                return true;
            case R.id.save:
                GregorianCalendar gDate = new GregorianCalendar(2012, 9, 11, 7, 13);
                childAdapter.addItem(new Child("Amira", "Abdusalamova", gDate.getTime()));

                result = "saved: " + item.getTitle().toString();
                break;
            case R.id.new_measure_save_button:
                saveMeasure();
                break;
            default:
                result = "clicked: " + item.getTitle().toString();
                break;
            //return super.onOptionsItemSelected(item);
        }
        mText.setText(result);
        return true;
    }

    /**
     * @since 26.02.13 14:58
     */
    protected void addChildLayout() {
        Intent intent = new Intent(this, AddChildActivity.class);
        startActivityForResult(intent, FOR_CHILD_ACTIVITY_RESULT);
    }

    /**
     * @since 26.02.13 14:58
     */
    protected void resultMeasures() {
        Intent intent = new Intent(this, MeasuresActivity.class);
        startActivityForResult(intent, FOR_MEASURES_ACTIVITY_RESULT);
    }

    protected void saveMeasure() {
        Measure measure = new Measure(
            getIntByResId(R.id.measure_height, this),
            getIntByResId(R.id.measure_girth_head, this),
            getIntByResId(R.id.measure_weight, this),
            getDateByResId(R.id.measure_date, this));
        measure.setChild(mSelectedChildId);
        MeasureAdapter adapter = new MeasureAdapter(this);
        adapter.addItem(measure);
    }

    /**
     *
     * @param resId
     * @return
     */
    public static Integer getIntByResId(int resId, Activity activity) {
        return Integer.getInteger(((EditText)activity.findViewById(resId)).getText().toString());
    }

    /**
     *
     * @param resId
     * @return
     */
    public static String getStringByResId(int resId, Activity activity) {
        return ((EditText)activity.findViewById(resId)).getText().toString();
    }

    /**
     *
     * @param resId
     * @return
     */
    public static Date getDateByResId(int resId, Activity activity) {
        DatePicker picker = (DatePicker)activity.findViewById(resId);
        GregorianCalendar gDate = new GregorianCalendar(picker.getYear(), picker.getMonth(), picker.getDayOfMonth());
        return gDate.getTime();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case FOR_CHILD_ACTIVITY_RESULT:
                if (resultCode == Activity.RESULT_OK) {
                    long childId = data.getLongExtra(KEY_FOR_RESULT_CHILD_ACTIVITY, 0L);
                    setDefaultChild(childId);
                }
                break;
            case FOR_MEASURES_ACTIVITY_RESULT:
                if (resultCode == Activity.RESULT_OK) {
                    long resId = data.getLongExtra(KEY_FOR_RESULT_MEASURES_ACTIVITY, 0L);
                    //todo
                }
                break;
        }
    }

    protected void setDefaultChild(int childId) {
        mSelectedChildId = (long)childId;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            invalidateOptionsMenu();
        }
    }

    public void setDefaultChild(Long childId) {
        setDefaultChild(childId.intValue());
    }
}
